package com.guobox.movies.utils;

import android.graphics.Color;

import com.guobox.movies.entity.BgColors;

/**
 * Created by xuxinlei on 2017/7/6.
 */

public class BgColorsUtils {

    public static BgColors getBgColors(String[] colors) {
        BgColors bgColors = new BgColors();
        if (colors == null) {
            colors = new String[]{"000000", "000000", "000000", "000000", "000000"};
            bgColors.setColors(colors);
            bgColors.setMainTextColor(Color.WHITE);
            bgColors.setSubTextColor(Color.GRAY);
        } else {
            bgColors.setColors(colors);
            bgColors.setMainTextColor(mainTextColor(colors[0]));
            bgColors.setSubTextColor(subTextColor(colors[0]));
        }
        return bgColors;
    }

    private static int mainTextColor(String color) {
        String r = color.substring(0, 2);
        String g = color.substring(2, 4);
        String b = color.substring(4, 6);
        int rgbcolor = Color.argb(255, Integer.parseInt(r, 16), Integer.parseInt(g, 16), Integer.parseInt(b, 16));
        float[] hsv = new float[3];
        Color.colorToHSV(rgbcolor, hsv);
        if (hsv[2] > 0.5f && hsv[1] <= 0.5f) {  // 2
            hsv[2] = 0.1f;
            hsv[1] = 0.9f;
        } else {    // 1, 3, 4
            hsv[2] = 0.9f;
            hsv[1] = 0.1f;
        }

        return Color.HSVToColor(hsv);
    }

    private static int subTextColor(String color) {
        String r = color.substring(0, 2);
        String g = color.substring(2, 4);
        String b = color.substring(4, 6);
        int rgbcolor = Color.argb(255, Integer.parseInt(r, 16), Integer.parseInt(g, 16), Integer.parseInt(b, 16));
        float[] hsv = new float[3];
        Color.colorToHSV(rgbcolor, hsv);
        if (hsv[2] > 0.5f && hsv[1] <= 0.5f) {  // 2
            hsv[2] = 0.2f;
            hsv[1] = 0.8f;
        } else {    // 1, 3, 4
            hsv[2] = 0.8f;
            hsv[1] = 0.2f;
        }

        return Color.HSVToColor(hsv);
    }


    public static String paddingColorString(int color) {
        String colorString = Integer.toHexString(color);
        if (colorString.length() < 2) {
            colorString = "0" + colorString;
        }
        return colorString;
    }
}
